package testcase;

import static org.junit.Assert.*;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import view.DisplayView;


/**
 * @author Lo Ng (52431461), Lusinda Leung (52571783)
 * @version for DisplayViewTest.java v3.1.5
 * @since 09-04-2013
 */
public class DisplayViewTest {

	private final ByteArrayOutputStream OUT = new ByteArrayOutputStream();
	private final ByteArrayOutputStream ERR = new ByteArrayOutputStream();
	private final String ENTER = System.lineSeparator();

	public DisplayViewTest() {
		super();
	}

	@Before
	public void setUpStreams() {
		System.setOut(new PrintStream(OUT));
		System.setErr(new PrintStream(ERR));
	}

	@After
	public void cleanUpStreams() {
		System.setOut(null);
		System.setErr(null);
	}

	@Before
	public void setUp() {
	}

	@After
	public void tearDown() {
	}

	// TEST for displayInvalidInputMsg()
	@Test
	public void testDisplayInvalidInputMsg() {
		DisplayView MDV = new DisplayView();
		MDV.displayInvalidInputMsg();
		assertEquals("The data provided is invalid. " + ENTER + "",
				OUT.toString());
	}

	// TEST for displayInvalidInputFormatMsg()
	@Test
	public void testDisplayInvalidInputFormatMsg() {
		DisplayView MDV = new DisplayView();
		MDV.displayInvalidInputFormatMsg();
		assertEquals("The format of data provided is inappropriate. " + ENTER
				+ "", OUT.toString());
	}

	// TEST for displayNotReadyMsg()
	@Test
	public void testDisplayNotReadyMsg() {
		DisplayView MDV = new DisplayView();
		MDV.displayNotReadyMsg();
		assertEquals(ENTER + "The system is not yet ready for customer."
				+ ENTER + "", OUT.toString());
	}

	// TEST for displayTerminatMsg()
	@Test
	public void testDisplayTerminatMsg() {
		DisplayView MDV = new DisplayView();
		MDV.displayTerminateMsg();
		assertEquals(ENTER + ENTER + "The system is terminated." + "",
				OUT.toString());
	}

	// TEST for displayInvalidOptionMsg()
	@Test
	public void testDisplayInvalidOptionMsg() {
		DisplayView MDV = new DisplayView();
		MDV.displayInvalidOptionMsg();
		assertEquals("INVALID OPTION" + ENTER + "", OUT.toString());
	}

	// TEST for displayFileInHandlingMsg(int)
	@Test
	public void testDisplayFileInHandlingMsg() {
		DisplayView MDV = new DisplayView();
		MDV.displayFileInHandlingMsg(1);
		MDV.displayFileInHandlingMsg(2);
		MDV.displayFileInHandlingMsg(3);
		MDV.displayFileInHandlingMsg(4);
		MDV.displayFileInHandlingMsg(5);
		assertEquals("Importing data..." + ENTER + "" + "No previous data."
				+ ENTER + "" + "Previous data are damaged." + ENTER + ""
				+ ENTER + "Please contact with technical staff." + ENTER + ""
				+ "Successfully import data." + ENTER + "", OUT.toString());
	}

	// TEST for displayFileOutHandlingMsg(boolean)
	@Test
	public void testDisplayFileOutHandlingMsg() {
		DisplayView MDV = new DisplayView();
		MDV.displayFileOutHandlingMsg(false);
		MDV.displayFileOutHandlingMsg(true);
		assertEquals(ENTER + ENTER + "Fail to export data." + "" + ENTER
				+ ENTER + "Successfully export data." + "", OUT.toString());
	}

	// TEST for displayModification(int)
	@Test
	public void testDisplayModification() {
		DisplayView MDV = new DisplayView();
		MDV.displayModification(-2);
		MDV.displayModification(0);
		MDV.displayModification(-1);
		assertEquals(ENTER + "No modification has been made." + ENTER + ENTER
				+ "Successfully modified the schedule." + ENTER
				+ "The modified line is assigned to Pier 01." + ENTER + ENTER
				+ "Fail to modify the schedule." + ENTER
				+ "No pier is available for the modified line." + ENTER,
				OUT.toString());
	}

	// TEST for displayRegistration(int)
	@Test
	public void testDisplayRegistration() {
		DisplayView MDV = new DisplayView();
		MDV.displayRegistration(0);
		MDV.displayRegistration(-1);
		assertEquals(ENTER + "Successfully registered the schedule." + ENTER
				+ "The new line is assigned to Pier 01." + ENTER + ENTER
				+ "Fail to register the schedule." + ENTER
				+ "No pier is available for the new line." + ENTER,
				OUT.toString());
	}

	// TEST for displayDeletion(boolean, boolean)
	@Test
	public void testDisplayDeletion() {
		DisplayView MDV = new DisplayView();
		MDV.displayDeletion(false, false);
		MDV.displayDeletion(true, true);
		MDV.displayDeletion(true, false);
		assertEquals(ENTER + "Request to delete the schedule is denied."
				+ ENTER + ENTER + "Successfully deleted the schedule." + ENTER
				+ ENTER + "Fail to delete the schedule." + ENTER,
				OUT.toString());
	}

	// TEST for fill(int)
	@Test
	public void testFill() {
		class MDVStub extends DisplayView {
			public String publicFill(int time) {
				return fill(time);
			}
		}
		MDVStub mDVStub = new MDVStub();
		assertEquals(mDVStub.publicFill(7), "07");
		assertEquals(mDVStub.publicFill(17), "17");
	}

	// TEST for printSchedule()
	@Test
	public void testPrintSchedule() throws UnsupportedEncodingException {
		DisplayView MDV = new DisplayView();
		int lineInfo[] = new int[5];
		lineInfo[0] = 800;
		lineInfo[1] = 60;
		lineInfo[2] = 5;
		lineInfo[3] = 30;
		lineInfo[4] = 1;
		MDV.printSchedule(lineInfo, 1, 2, 3);
		assertEquals(ENTER + "Pier 02 (Size: Large)\tLine 03 (Size: Small)"
				+ ENTER + "+-------+----------------+------------------+"
				+ ENTER + "|  Run  |  Arrival Time  |  Departure Time  |"
				+ ENTER + "|-------+----------------+------------------|"
				+ ENTER + "|   1   |      08:00     |       08:30      |"
				+ ENTER + "|   2   |      09:00     |       09:30      |"
				+ ENTER + "|   3   |      10:00     |       10:30      |"
				+ ENTER + "|   4   |      11:00     |       11:30      |"
				+ ENTER + "|   5   |      12:00     |       12:30      |"
				+ ENTER + "+-------+----------------+------------------+"
				+ ENTER, OUT.toString());
	}
}
